package com.boang.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.boang.entity.PO.Reservation;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 22567
 * @description 针对表【reservation】的数据库操作Mapper
 * @createDate 2025-03-10 15:47:56
 * @Entity com.boang.entity.PO.Reservation
 */
public interface ReservationMapper extends BaseMapper<Reservation> {
    /**
     * 插入预约记录
     */
    @Insert("INSERT INTO reservation (user_id, venue_id, start_time, end_time, status) " +
            "VALUES (#{userId}, #{venueId}, #{startTime}, #{endTime}, #{status})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertReservation(Reservation reservation);

    /**
     * 检测时间冲突
     */
    @Select("SELECT * FROM reservation " +
            "WHERE venue_id = #{venueId} " +
            "AND status != '已取消' " +
            "AND (start_time < #{end} AND end_time > #{start})")
    List<Reservation> checkTimeConflict(
            @Param("venueId") Long venueId,
            @Param("start") LocalDateTime start,
            @Param("end") LocalDateTime end
    );

    /**
     * 取消预约（更新状态）
     */
    @Update("UPDATE reservation SET status = '已取消' WHERE id = #{id}")
    int cancelReservation(Long id);
    // 原有方法...

    @Select("SELECT r.*, v.name as venue_name FROM reservation r " +
            "LEFT JOIN venue v ON r.venue_id = v.id")
    List<Reservation> selectAllReservations();

    @Select("SELECT r.*, v.name as venue_name FROM reservation r " +
            "LEFT JOIN venue v ON r.venue_id = v.id " +
            "WHERE r.user_id = #{userId}")
    List<Reservation> selectReservationsByUserId(String userId);

    @Select("SELECT r.*, v.name as venue_name FROM reservation r " +
            "LEFT JOIN venue v ON r.venue_id = v.id " +
            "WHERE r.venue_id = #{venueId}")
    List<Reservation> selectReservationsByVenueId(Long venueId);
}




